<?php
class User{
    
    protected static $_instance = null;
    /**
     * @var Model_User
     */
    protected $_model;
    protected $_info = false;
    protected $_id = false;
    /**
     * @var Store_Session
     */
    protected $_session = false;

	static public function getInstance(){
        if(is_null(self::$_instance))
           self::$_instance = new self();
        return self::$_instance;   
    }	
    
    protected function __construct(){         
        $this->_model = Model_User::getInstance();
        $this->_session = Store_Session::getInstance();
        $this->_checkAuthSession();
    }
    /**
     * Set user id (causes reset of user info)
     * @param integer $id
     * @return void
     */
    public function setId($id){
        $this->_id = $id;
        $this->_info = null;
    }
    /**
     * Load user information
     * @throws Exception
     */
    protected function _loadData(){
        
        if(!$this->_id)
            throw new Exception('Invalid user id');
        
        $data = $this->_model->getInfo($this->_id); 
        if(!$data)
            throw new Exception('Invalid user data'); 
            
        $this->setInfo($data);   
    }
    /**
     * Set user information
     * @param array $data
     */
    public function setInfo(array $data){
        $this->_info = $data;
    }
    /**
     * Get user information
     * @return array
     */
    public function getInfo(){
        return $this->_info;
    }
 
    public function __get($val)
    {
         if($val ==='id')
             return $this->_id;
             
        if(is_null($this->_info))
            $this->_loadData();    
        
        if($val ==='id')
             return $this->_id;
         
        if(isset($this->_info[$val]))
            return $this->_info[$val];
        else
            throw new Exception('User. Invalid property "'.$val.'" ');    
    }
	/**
     * Check if user is authorized
     * @return mixed  - user_id or false
     */
    static public function isAuthorized(){
        $ses = Store::factory(Store::Session);
        
        if($ses->keyExists('auth') && $ses->get('auth'))
            return $ses->get('auth_id');
        else 
            return false;    
    }
    /**
     * Check if user is administrator
     * @return boolean
     */
    static public function isAdmin(){
        $ses = Store::factory(Store::Session);
        
        if($ses->keyExists('admin') && $ses->get('admin'))
            return true;
        else 
            return false;    
    }
    /**
     * Set authorisation info.
     * @param boolean $admin - optional
     * @return void
     */
    public function setAuthorised($admin = false){
         $ses = Store::factory(Store::Session);
         $ses->set('auth' , true);
         $ses->set('auth_id' , $this->_id);
         if($admin)
             $ses->set('admin', true);
    }
    /**
     * Check if user auth session exists 
     */
    protected function _checkAuthSession(){
         $ses = Store::factory(Store::Session);
         if($ses->keyExists('auth') && $ses->get('auth') && $ses->keyExists('auth_id') && $ses->get('auth_id'))
             $this->setId($ses->get('auth_id'));
    }
}